06. Adding Fetch to Your Code

Adding Fetch to Your Code Header

Adding Fetch to Your Code

ND#0001 C3 L4 A04 Async Fetch W- APIs (3 Of 3)-

Async Fetch w/ Web APIs Recap

Here is the client side code that would make a GET request to the animal info API:

let baseURL = 'http://api.animalinfo.org/data/?animal='
let apiKey = '&appid=9f15e45060...';

document.getElementById('generate').addEventListener('click', performAction);

function performAction(e){
const newAnimal =  document.getElementById('animal').value;
getAnimal(baseURL,newAnimal, apiKey)

}
const getAnimal = async (baseURL, animal, key)=>{

  const res = await fetch(baseURL+animal+key)
  try {

    const data = await res.json();
    console.log(data)
    return data;
  }  catch(error) {
    console.log("error", error);
    // appropriately handle the error
  }
}